package handler

import (
	"github.com/gin-gonic/gin"
	"pic-text-mng-sys/models"
	"pic-text-mng-sys/pkg"
	"strconv"
)

func Login(c *gin.Context) {
	tel := c.Query("tel")
	password := c.Query("password")
	if tel == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}
	var user models.User
	err := models.DB.Where("tel = ?", tel).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	}
	if pkg.Md5str(password) != user.Password {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	token := pkg.TokenHandler(strconv.Itoa(int(user.ID)))
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "登录成功",
		"token": token,
	})
	return
}
